package com.fourforfo.fourmall.order.listener;

import com.fourforfo.common.to.mq.SeckillOrderTO;
import com.fourforfo.fourmall.order.config.MyOrderMQConfig;
import com.fourforfo.fourmall.order.constant.OrderConstant;
import com.fourforfo.fourmall.order.service.OrderService;
import com.fourforfo.fourmall.order.to.OrderCreateTO;
import com.fourforfo.fourmall.order.vo.OrderSubmitResponseVO;
import com.fourforfo.fourmall.order.vo.OrderSubmitVO;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @program: xxl-sso-core
 * @description: 秒杀商品订单创建
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-09-16 17:18
 **/
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    OrderService orderService;

    /**
     * 创建秒杀订单
     * @param seckillOrderTO
     * @param message
     * @param channel
     */
    @RabbitHandler
    public void seckillOrderCreate(SeckillOrderTO seckillOrderTO, Message message, Channel channel) throws IOException {
        Boolean isSuccess = orderService.createSeckillOrder(seckillOrderTO);
        if(isSuccess){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }else{
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }


    }


}
